/// Text Overlay Application: Tracker Class
/// This class contains functions necessary to Log and Track the application's 
/// activity. These functions handle both the on screen logging process and the 
/// logging to a file process.

using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Microsoft.DirectX.Direct3D;

namespace TextOverlay
{
    struct Track
    {
        private static LogBuffer Log = new LogBuffer(5);
        private static string Path;
        private static System.Object mrlock = new System.Object();
        private static StreamWriter SWriter;
        
        public static int error = 0;
        public static int swap = 0;

        /// <summary>
        /// This function distributes and stores all logging information in the application. All log messages are
        /// sent to the log buffer (which is displayed on the screen if the application is running on log mode), written
        /// to the application log file and written to the debug console.
        /// </summary>
        /// <param name="message">string message to log</param>
        public static void LogInfo(string message)
        {
            Log.AppendToLog(message);
            WriteToTextLog(message);
            Console.WriteLine(message);
        }

        /// <summary>
        /// this function calls the Render function in the log structure
        /// </summary>
        /// <param name="font">font to render the log on the screen</param>
        /// <param name="sprite">sprite to render the log on the screen</param>
        /// <param name="x">x position of the log</param>
        /// <param name="y">y position of the log</param>
        public static void RenderLog(Font font, Sprite sprite, int x, int y)
        {
            Log.RenderLog(sprite, font, x, y);
        }

        /// <summary>
        /// this function writes a string message into the application's log file. Every time this function is called
        /// it checks if the current size of the log file is bigger than 512kb, if the file is bigger than that size,
        /// the function creates a new log file to continue logging.
        /// </summary>
        /// <param name="message">string to log</param>
        public static void WriteToTextLog(string message)
        {
            lock (mrlock)
            {
                try
                {
                    if (GetFileSize(Path) > 512)
                        CreateNewLog();
                    SWriter = File.AppendText(Path);
                    SWriter.WriteLine(DateTime.Now.ToLongTimeString() + " -> " + message);
                    SWriter.Close();
                }
                catch
                {
                    Log.AppendToLog("Error Attempting to Write Log");
                }
            }
        }

        /// <summary>
        /// this function is used by the WriteToTextLog() function to check the size of the current 
        /// log file. it returns the size of the file in kb
        /// </summary>
        /// <param name="path">the absolute path of the target file</param>
        /// <returns>the size of the file in kilobytes</returns>
        public static int GetFileSize(string path)
        {
            FileInfo file = new FileInfo(path);
            return Convert.ToInt32(file.Length / 1024);
        }


        /// <summary>
        /// this function is used by the WriteToTextLog() function to create a new log file each time
        /// the current log file reaches the size 512kb. This function will also be called when the application
        /// initializes to create the first log file.
        /// </summary>
        public static void CreateNewLog()
        {      
            try
            {
                if (Directory.Exists("C:/TextOverlay Logs/") != true)
                    Directory.CreateDirectory("C:/TextOverlay Logs");
                Path = String.Format("C:/TextOverlay Logs/Log For {0:MMMM dd (hh.mm.tt)}.txt", DateTime.Now);
                if (File.Exists(Path))
                    File.Delete(Path);
                FileStream file = new FileStream(Path, FileMode.Create, FileAccess.Write);
                file.Close();
            }
            finally { }   
        }
    }
}
